<template>
  <view>
    <my_search @click.native="goSearch" radius="16"></my_search>
   <view class="scroll_container">
     <!-- 左侧滚动条 -->
     <scroll-view class="left" scroll-y="true" :style="{height: wh+'px'}">
       <view @click="changeState(i)" class="scroll_view_item" :class="{active:i===currentIndex}"
         v-for="(item,i) in cateList" :key="i">{{item.cat_name}}</view>
     </scroll-view>
     <!-- 右侧滚动条 -->
     <scroll-view scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
       <template>
         <view class="right" v-for="(item2,i2) in cateLevel2" :key="i2" >
           <view class="scroll_title">/ {{item2.cat_name}} /</view>
           <view class="cate_three">
             <view class="cate_three_item" v-for="(item3,i3) in item2.children" :key="i3" @click="goGoodList(item3)">
               <image :src="item3.cat_icon" mode=""></image>
               <text>{{item3.cat_name}}</text>
             </view>
           </view>
         </view>
       </template>
     </scroll-view>
   </view> 
  </view>

</template>

<script>
 import cartBadge from '@/mixins/tabbar-badge.js'
 export default {
   mixins:[cartBadge],
    data() {
      return {
        wh: 0, //可用窗口的高度
        cateList: [], //分类列表数据;
        cateLevel2: [], //2级分类
        currentIndex: 0, //控制选择了哪个
        scrollTop: 0, //针对滚动条距离bug
      };
    },
    onLoad() {
      this.getSysHeight(), //获取可用高度
        this.getCateList()
    },
    methods: {
      // 获取可用视口高度
      getSysHeight() {
        const val = uni.getSystemInfoSync()
        // 要减去搜索框的高度
        this.wh = val.windowHeight-50
        // console.log(val);
      },
      // 获取分类列表数据
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories');
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message;
        this.cateLevel2 = res.message[0].children;
        // console.log(res);
      },
      // 改变选中状态
      changeState(i) {
        this.currentIndex = i;
        this.cateLevel2 = this.cateList[i].children;
        this.scrollTop = (this.scrollTop === 0 ? 1 : 0); //每次切换一级菜单时让滚动条重置,因为初始值为0,再重置为0会有问题
      },
      goGoodList(item){
        console.log(item);
       uni.navigateTo({
       	url: '/subpkg/goods_list/goods_list?cid='+item.cat_id
       });
      },
      // 点击搜索跳转到搜索页面
      goSearch(){
      uni.navigateTo({
        url:"/subpkg/search/search"
      })
      }
    },
  }
</script>

<style lang="scss" scoped>
  .scroll_container {
    display: flex;

    .left {
      width: 120px;

      .scroll_view_item {
        line-height: 60px;
        background-color: #f7f7f7;
        text-align: center;
      }

      .active {
        position: relative;
        background-color: #ffffff;

        &::before {
          content: "";
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          display: block;
          width: 3px;
          height: 30px;
          background-color: #C00000;
        }
      }
    }

    .right {
      .scroll_title {
        font-size: 12px;
        padding: 15px 0;
        text-align: center;
        font-weight: bold;
      }

      .cate_three {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;

        .cate_three_item {
          display: flex;
          width: 33.3%;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          margin-bottom: 10px;

          image {
            height: 60px;
            width: 60px;
          }

          text {
            font-size: 12px
          }
        }
      }
    }
  }
</style>
